package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.RoleMapper;
import com.dongdongmovie.mapper.RoleResourceMapper;
import com.dongdongmovie.pojo.Role;
import com.dongdongmovie.pojo.RoleResourceExample;
import com.dongdongmovie.service.RoleResource_Service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class RoleResource_ServiceImpl implements RoleResource_Service {

    //注入mapper
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private RoleMapper roleMapper;

    //查询全部
    @Override
    public List<Role> show() {
        return roleMapper.selectByExample(null);
    }

    //根据id查找数据
    @Override
    public Role showbyId(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    //添加数据
    @Override
    public int add(Role r, Integer[] roleIds) {
        roleMapper.add(r);
        roleResourceMapper.add(r.getRoleId(),roleIds);
        return 1;
    }

    @Override
    public int update(Role r, Integer[] roleIds) {
        roleMapper.updateByPrimaryKeySelective(r);
        RoleResourceExample re = new RoleResourceExample();
        RoleResourceExample.Criteria criteria = re.createCriteria();
        criteria.andRoleIdEqualTo(r.getRoleId());
        roleResourceMapper.deleteByExample(re);
        roleResourceMapper.add(r.getRoleId(),roleIds);
        return 1;
    }

    //删除数据
    @Override
    public int delete(Integer roleId) {
        roleMapper.deleteByPrimaryKey(roleId);
        RoleResourceExample re = new RoleResourceExample();
        RoleResourceExample.Criteria criteria = re.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(re);
        return 1;
    }
}
